package com.abe.bathsystem.utils;

import static com.abe.bathsystem.utils.ByteUtils.byteToInt;

public class SerialPortUtils {
    //串口相关
    public static final int SerialPort_ttys0 = 0x001;
    public static final int SerialPort_ttysOther = 0x002;
    public static final int baudRate = 115200;
    public static final String path_ttys0 = "/dev/ttyS1";
    //温度限制
    private final static int TEMPERATURE_LIMIT = byteToInt((byte) 0xFF) * 256 + byteToInt((byte) 0xFF);
    //******************************************************************
    public static final String TYPE_COMMAND_WATER_CLOSE="getWaterValveC-close";
    public static final String TYPE_COMMAND_WATER_OPEN="getWaterValveC-open";
    public static final String TYPE_COMMAND_WATER_READ_USAGE="getReadWaterUsageC";
    public static final String TYPE_COMMAND_WATER_CLEAR_USAGE="getClearWaterUsageC";
    public static final String TYPE_COMMAND_LOCKER_CLOSE="getLockerC-close";
    public static final String TYPE_COMMAND_LOCKER_OPEN="getLockerC-open";
    public static final String TYPE_COMMAND_READ_LOCKER_STATE="getReadLockerStateC";
    public static final String TYPE_COMMAND_READ_TEMP="getReadTemperatureC";

    //*************************** 命令集合 ******************************/
    public static final byte type_read = 0x03;
    public static final byte type_write = 0x10;
    public static final byte type_read_control = 0x01;
    public static final byte type_control = 0x05;

    //读温度
    public static byte[] getReadTemperatureC = CRCChecker.getSendBuf(new byte[]{0x01, type_read, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00});

    public static byte[] getSetAddressC(int pos) {
        return CRCChecker.getSendBuf(new byte[]{0x01, type_write, 0x10, 0x01, 0x00, 0x01, 0x02, 0x00, getAddress(pos), 0x00, 0x00});
    }

    //按号位读浴位用水量
    public static byte[] getReadWaterUsageC(int pos) {
        return CRCChecker.getSendBuf(new byte[]{0x01, type_read, 0x00, getAddress(pos), 0x00, 0x01, 0x00, 0x00});
    }

    //按号位清空浴位用水量
    public static byte[] getClearWaterUsageC(int pos) {
        return CRCChecker.getSendBuf(new byte[]{0x01, type_write, 0x00, getAddress(pos), 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00});
    }

    //按号位读取水阀状态
    public static byte[] getReadWaterValveStateC(int pos) {
        return CRCChecker.getSendBuf(new byte[]{0x01, type_read_control, 0x00, getAddress(pos), 0x00, 0x01, 0x00, 0x00});
    }

    //按号位读取储物柜锁状态
    public static byte[] getReadLockerStateC(int pos) {
        return CRCChecker.getSendBuf(new byte[]{0x01, type_read_control, 0x01, getAddress(pos), 0x00, 0x01, 0x00, 0x00});
    }

    //按号位打开或者关闭水阀
    public static byte[] getWaterValveC(int pos, boolean isOpen) {
        byte control = isOpen ? (byte) 0xFF : (byte) 0x00;
        return CRCChecker.getSendBuf(new byte[]{0x01, type_control, 0x00, getAddress(pos), 0x00, control, 0x00, 0x00});
    }

    //按号位打开或者关闭储物柜锁
    public static byte[] getLockerC(int pos, boolean isOpen) {
        byte control = isOpen ? (byte) 0xFF : (byte) 0x00;
        return CRCChecker.getSendBuf(new byte[]{0x01, type_control, 0x01, getAddress(pos), 0x00, control, 0x00, 0x00});
    }

    private static byte getAddress(int pos) {
        byte[] temp = ByteUtils.IntToByteArray2(pos);
        return temp[temp.length - 1];
    }
    //*************************** 方法集合 ******************************/

    //转换供水量（升）
    public static int getWaterUsage(byte[] data) {
        int x = 0;
        if (data.length == 2) {
            x = byteToInt(data[0]) * 256 + byteToInt(data[1]);
        }
        return x;
    }

    //转换检测温度
    public static float getTemperature(byte[] data) {
        int x = 0;
        float y;
        if (data.length == 2) {
            x = byteToInt(data[0]) * 256 + byteToInt(data[1]);
        }
        if (x * 2 < TEMPERATURE_LIMIT) {
            y = (float) x / (float) 10;
        } else {
            y = (float) (x - TEMPERATURE_LIMIT) / (float) 10;
        }
        return y;
    }
}